package com.sdry.approval.controller;

import com.sdry.approval.entity.LeaveMan;
import com.sdry.approval.entity.LeaveRequest;
import com.sdry.approval.service.LeaveRequestService;
import com.sdry.approval.service.MeSponsorService;
import com.sdry.common.BaseController;
import com.sdry.untils.UserContext;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @program: info_sdry
 * @description: 请假申请
 * @author: cb
 * @create: 2020-02-21 16:00
 **/
@Controller
@RequestMapping(value = "/leaveRequest")
public class LeaveRequestController extends BaseController {

    @Autowired
    private LeaveRequestService leaveRequestService;


    /**
     * 添加
     * @param leaveRequest 申请对象
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public boolean add(LeaveRequest leaveRequest,@RequestParam("userId[]") String[] userId) {
        logger.info("请假申请添加成功 leaveRequest = {}"+ JSONObject.fromObject(leaveRequest));
        return  leaveRequestService.add(leaveRequest,userId);
    }

    /**
     * 查询详细
     * @return
     */
    @RequestMapping(value = "/queryLeave/{requestId}")
    public String queryLeave(@PathVariable("requestId") Long requestId, Model model) {
        LeaveRequest leaveRequest = leaveRequestService.getById(requestId);
        if ("sj".equals(leaveRequest.getLeaveRequestType())){
            leaveRequest.setLeaveRequestType("事假");
        }else {
            leaveRequest.setLeaveRequestType("病假");
        }

        List<LeaveMan> leaveMEN = leaveRequestService.queryLeaveManList(requestId, "qj");
        if (leaveMEN != null){
            for (LeaveMan leaveMAN : leaveMEN) {
                if (leaveMAN.getUserId().equals(UserContext.getSessionUserIdByServiceImpl())){
                    leaveRequest.setProposal(leaveMAN.getProposal());
                }
            }
        }
        model.addAttribute("leaveRequest",leaveRequest);
        return "/approval/detail/leave";
    }


    /**
     * 查询详细
     * @return
     */
    @RequestMapping(value = "/queryLeaveDetail/{requestId}")
    public String queryLeaveDetail(@PathVariable("requestId") Long requestId, Model model) {
        LeaveRequest leaveRequest = leaveRequestService.getById(requestId);
        if ("sj".equals(leaveRequest.getLeaveRequestType())){
            leaveRequest.setLeaveRequestType("事假");
        }else {
            leaveRequest.setLeaveRequestType("病假");
        }

        List<LeaveMan> leaveMEN = leaveRequestService.queryLeaveManList(requestId, "qj");
        if (leaveMEN != null){
            for (LeaveMan leaveMAN : leaveMEN) {
                if (leaveMAN.getUserId().equals(UserContext.getSessionUserIdByServiceImpl())){
                    leaveRequest.setProposal(leaveMAN.getProposal());
                }
            }
        }
        model.addAttribute("leaveRequest",leaveRequest);
        return "/approval/detail/alreadyleave";
    }

    /**
     * 审批，填写审批意见
     */
    @RequestMapping(value = "/approval")
    @ResponseBody
    public boolean approval(String requestType,Long requestId,String proposal) {
        return leaveRequestService.approval(requestType,requestId,proposal);
    }
}
